home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 036a / pmfinder.zip / MISC.C < prev    next >
Text File  |  1991-12-18  |  4KB  |  166 lines

  1. #define INCL_PM
  2. #define NUL '\0'
  3. #include <os2.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <malloc.h>
  8.  
  9. #include "misc.h"
  10.  
  11.  extern  BOOL pascal fContinue;
  12.  
  13. /*
  14.     The Match functions match a pattern to a filename.
  15.     These functions have changed slightly from
  16.     Don A. Williams orginal code.
  17.     I've removed references to global variables.
  18.  *
  19.  */
  20.  int
  21. Match (char *Str, char *Pat) {
  22.    char S_Name[66], S_Ext[4];
  23.    char P_Name[66], P_Ext[4];
  24.    char *p1;
  25.  
  26.    if ( (p1 = strrchr(Str, '.')) != NULL ) {
  27.       *p1 = '\0';
  28.       strcpy(S_Name, Str);
  29.       strcpy(S_Ext, p1+1);
  30.       *p1 = '.';
  31.       }
  32.    else {
  33.       strcpy(S_Name, Str);
  34.       S_Ext[0] = '\0';
  35.       }
  36.  
  37.    if ( (p1 = strchr(Pat, '.')) != NULL ) {
  38.       *p1 = '\0';
  39.       strcpy(P_Name, Pat);
  40.       strcpy(P_Ext, p1+1);
  41.       *p1 = '.';
  42.       }
  43.    else {
  44.       strcpy(P_Name, Pat);
  45.       strcpy(P_Ext, "*");
  46.       }
  47.  
  48.    if ( !I_Match(S_Name, P_Name) ) return(0);
  49.    if ( (P_Ext[0] == '\0') && (S_Ext[0] != '\0') ) return(0);
  50.    if ( !I_Match(S_Ext, P_Ext) ) return(0);
  51.    return(1);
  52.    }
  53.  
  54.  
  55.  static int
  56. I_Match (char *Str, char *Pat) {
  57.    char *p, *p1, *p2;
  58.    int t;
  59.  
  60.    if ( (p1 = strchr(Pat, '*')) == NULL)
  61.       return( S_Match(Str, Pat, 1) );
  62.    if (Pat[0] != '*') {
  63.       *p1 = '\0';
  64.       t = S_Match(Str, Pat, 0);
  65.       *p1 = '*';
  66.       if (!t) return(0);
  67.       }
  68.    if (Pat[strlen(Pat)-1] != '*') {
  69.       p2 = strrchr(Pat, '*') + 1;
  70.       if ( !S_Match(&Str[strlen(Str) - strlen(p2)], p2, 1) )
  71.          return(0);
  72.       }
  73.  
  74.    p = Str;
  75.    while ( (p2 = strchr(++p1, '*')) != NULL ) {
  76.       *p2 = '\0';
  77.       while ( (p = strchr(p, p1[0])) != NULL ) {
  78.          if ( S_Match(p, p1, 0) ) break;
  79.          ++p1;
  80.          }
  81.       if (p == NULL) return(0);
  82.       p += strlen(p1);
  83.       *p2 = '*';
  84.       p1 = p2;
  85.       }
  86.    return(1);
  87.    }
  88.  
  89.  
  90.  static int
  91. S_Match (char *S, char *P, int Anchor) {
  92.  
  93.    while (*P != '\0') {
  94.       if ( (*S == *P) || (*P == '?') ) {
  95.          S++;
  96.          P++;
  97.          }
  98.       else return(0);
  99.       }
  100.    if ( Anchor && (*S != '\0') ) return(0);
  101.    return(1);
  102.    }
  103.  
  104. /*
  105.  * AddToListBox( hwndListBox, szEntry    ) : VOID;
  106.  *
  107.  *    hwndListBox    handle to output listbox
  108.  *    szEntry        character sting to insert
  109.  *
  110.  * This function adds the string to the listbox and if not enough
  111.  * memory, will issue an error message box.
  112.  *
  113.  */
  114. void AddToListBox(HWND hwndListBox,char *szEntry)
  115. {
  116. SHORT      sErr;            /* error code               */
  117.        if (fContinue)
  118.        {
  119.            sErr = SHORT1FROMMR( WinSendMsg(
  120.              hwndListBox,
  121.              LM_INSERTITEM,
  122.              MPFROMSHORT( LIT_END ),
  123.              szEntry));
  124.            switch ( sErr )
  125.            {
  126.              case LIT_MEMERROR:
  127.                WinMessageBox(
  128.                  HWND_DESKTOP,
  129.                  WinQueryWindow( hwndListBox, QW_PARENT, FALSE ),
  130.                  "Too many files!  Narrow the search criteria!",
  131.                  "File Finder",
  132.                  0,
  133.                  MB_OK | MB_ICONHAND
  134.                );
  135.                fContinue = FALSE;
  136.                break;
  137.  
  138.              case LIT_ERROR:
  139.                WinMessageBox(
  140.                  HWND_DESKTOP,
  141.                  WinQueryWindow( hwndListBox, QW_PARENT, FALSE ),
  142.                  "Unable to Add Item to List Box!",
  143.                  "File Finder",
  144.                  0,
  145.                  MB_OK | MB_ICONHAND
  146.                );
  147.                fContinue = FALSE;
  148.                break;
  149.  
  150.              default:
  151.                break;
  152.  
  153.            }
  154.        }
  155. }
  156.  
  157. void *zalloc(size_t bytes)
  158. {
  159.     void *ret = malloc(bytes);
  160.     if(!ret){
  161.         printf("Memory request for %u bytes failed\n", bytes);
  162.         exit(3);
  163.     }
  164.     return ret;
  165. }
  166.